package com.triangle.cashier.widget;


import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;

import com.triangle.cashier.R;
import com.triangle.cashier.adapter.PendingOrderAdapter;
import com.triangle.cashier.adapter.PendingOrderDetailAdapter;
import com.triangle.cashier.tools.Screen;

/**
 * 选择会员
 * Created by  on 2018/6/30.
 */

public class PendingOrderSystem extends DialogFragment implements View.OnClickListener {

    private OnPendingClickListener pendingResultClickListener;
    private ListView pendingOrderListView;
    private PendingOrderAdapter pendingOrderAdapter;

    private ListView pendingOrderDetailListView;
    private PendingOrderDetailAdapter pendingOrderDetailAdapter;
    private int selectIndex = 0;

    private ImageButton cancelBtn;
    private ImageButton deleteBtn;
    private ImageButton takeBtn;


    public PendingOrderSystem(OnPendingClickListener pendingResultClickListener) {
        this.pendingResultClickListener = pendingResultClickListener;

    }

    public PendingOrderSystem() {
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View customView = LayoutInflater.from(getActivity()).inflate(
                R.layout.pending_order_system, null);
        initView(customView);
        Dialog dialog = new Dialog(getActivity(), R.style.soft_para_dialog);
        dialog.setContentView(customView);
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
//        window.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_windows_bg));
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (Screen.getScreenWidth(getActivity()) * 0.7);   //设置宽度充满屏幕
        lp.height = (int) (Screen.getScreenHeight(getActivity()) * 0.8);
        window.setAttributes(lp);
        return dialog;
    }

    private void initView(View view) {
        ImageButton closeBtn = view.findViewById(R.id.close_btn);
        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pendingResultClickListener.cancel();
            }
        });
        pendingOrderListView = view.findViewById(R.id.order_listView);
        pendingOrderAdapter = new PendingOrderAdapter(getActivity());
        pendingOrderListView.setAdapter(pendingOrderAdapter);
        pendingOrderListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectIndex = position;
                pendingOrderAdapter.setSelectIndex(position);


            }
        });


        pendingOrderDetailListView = view.findViewById(R.id.detail_listView);
        pendingOrderDetailAdapter = new PendingOrderDetailAdapter(getActivity());
        pendingOrderDetailListView.setAdapter(pendingOrderDetailAdapter);
        cancelBtn = view.findViewById(R.id.cancel_button);
        deleteBtn = view.findViewById(R.id.delete_order_button);
        takeBtn = view.findViewById(R.id.take_order_button);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pendingResultClickListener.cancel();
            }
        });
        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pendingOrderAdapter.count > 0) {
                    pendingOrderAdapter.count =
                            pendingOrderAdapter.count - 1;
                }

                if (pendingOrderAdapter.count == 0) {
                    pendingOrderDetailAdapter.count = 0;
                    pendingOrderDetailAdapter.notifyDataSetChanged();
                }
                pendingOrderAdapter.notifyDataSetChanged();

            }
        });
        takeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pendingResultClickListener.ok(selectIndex);
            }
        });

    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onStart() {

        super.onStart();
        Window window = this.getDialog().getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        window.getDecorView().setSystemUiVisibility(uiOptions);

    }

    @Override
    public void onClick(View v) {

    }

    public interface OnPendingClickListener {

        void cancel();

        void delete();

        /**
         * 确定事件
         *
         * @param position 检索位置
         */
        void ok(int position);

    }

}
